package com.example.classconnect.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;

import com.example.classconnect.MainActivity;
import com.example.classconnect.R;
import com.example.classconnect.User;

public class ProfileFragment extends Fragment {
	Uri imageUri;
	String selectedPath;
	
	ImageView imgFavorite;
	TextView user;
	Button uploadButton;
	Context context;
	//Activity activity = getActivity();
	User currentUser;
	static final int UPLOAD_PICTURE = 1;
	static final int TAKE_PICTURE = 2;
	
	

	public ProfileFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		String value = "";
		currentUser = getArguments().getParcelable("CURRENTUSER");
		value = currentUser.getfName();
		View rootView = inflater.inflate(R.layout.fragment_profile, container,
				false);

		user = (TextView) rootView.findViewById(R.id.textView1);
		user.setText("Welcome " + value);
		imgFavorite = (ImageView) rootView.findViewById(R.id.imageView1);
		imgFavorite.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				open();
			}
		});
		uploadButton = (Button) rootView.findViewById(R.id.submitClass);
		uploadButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				find();
			}
		});
		
		return rootView;
	}

	public void open() {
		Intent intent = new Intent(
				android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(intent, TAKE_PICTURE);
	}

	public void find(){
		Intent intent = new Intent();  
		intent.setType("image/*");  
		intent.setAction(Intent.ACTION_GET_CONTENT);  
		startActivityForResult(Intent.createChooser(intent, "Choose Picture"), UPLOAD_PICTURE);
	}
	
	
	public String getPath(Uri uri) {
		 
        String[] projection = { MediaStore.Images.Media.DATA };
 
        try {
        Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
 
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
 
        cursor.moveToFirst();
 
        return cursor.getString(column_index);
        } catch (NullPointerException e) {
        	return "";
        }
 
    }
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			if(getActivity() != null)
				Toast.makeText(getActivity().getApplicationContext(), "No image selected", 500).show();
			return; 
		}
		
		if(data.getData() != null) {
			imageUri = data.getData();
			if(getActivity() != null)
				Toast.makeText(getActivity().getApplicationContext(), "selected : " + imageUri, 500).show();
		}
		
		
		if (requestCode == TAKE_PICTURE) {
			Bitmap bp = (Bitmap) data.getExtras().get("data");
			imgFavorite.setImageBitmap(bp);
			if(getActivity() != null)
				Toast.makeText(getActivity().getApplicationContext(), "Picture added", 500).show();
		}
		if(requestCode == UPLOAD_PICTURE){  
			//Uri uri = (Uri) data.getData();
			//imgFavorite.setImageURI(uri);
			
			selectedPath = getPath(imageUri);
			imgFavorite.setImageURI(imageUri);
			//imgFavorite.setScaleType(ImageView.ScaleType.FIT_XY);
			if(getActivity() != null)
				Toast.makeText(getActivity().getApplicationContext(), "Image uploaded!", 500).show();
			
        }
	}
	

}
